package com.yanqu.road.server.pb.banquet;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.entity.banquet.CrossPartyRelationship;
import com.yanqu.road.entity.banquet.cross.CrossBanquetData;
import com.yanqu.road.entity.banquet.cross.CrossBanquetMember;
import com.yanqu.road.entity.banquet.cross.CrossBanquetRecord;
import com.yanqu.road.entity.banquet.invitation.BanquetMarryInvitation;
import com.yanqu.road.entity.enums.eBanquetInfoType;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.banquet.BanquetProto;
import com.yanqu.road.pb.banquet.CrossBanquetProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.manager.banquet.CrossPartyMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.util.Collection;
import java.util.List;
import java.util.Map;


public class CrossBanquetPb {
    public static CrossBanquetProto.SyncCrossBanquetMsg.Builder parseSyncCrossBanquetMsg(CrossBanquetData crossBanquetData) {
        CrossBanquetProto.SyncCrossBanquetMsg.Builder msg = CrossBanquetProto.SyncCrossBanquetMsg.newBuilder();
        CrossBanquetProto.SyncCrossBanquetTemp.Builder temp = CrossBanquetProto.SyncCrossBanquetTemp.newBuilder();
        temp.setServerId(crossBanquetData.getServerId());
        temp.setUserId(crossBanquetData.getUserId());
        temp.setBanquetId(crossBanquetData.getBanquetId());
        temp.setType(crossBanquetData.getType());
        temp.setOpenTime(crossBanquetData.getOpenTime());
        temp.setOverTime(crossBanquetData.getOverTime());
        temp.setIsSendReward(crossBanquetData.isSendReward());
        temp.setPopularity(crossBanquetData.getPopularity());
        temp.setIntegral(crossBanquetData.getIntegral());
        temp.setUserCount(crossBanquetData.getUserCount());
        temp.setServerList(crossBanquetData.getServerListStr());
        temp.setInvitationId(crossBanquetData.getInvitationId());
        temp.setInvitationInfo(crossBanquetData.getInvitationInfo());
        temp.setStatus(crossBanquetData.getStatus());
        temp.setGiftTypeAccept(crossBanquetData.getGiftTypeAccept());
        temp.setMessage(crossBanquetData.getMessage());
        PlayerProto.PlayerBaseTempMsg.Builder baseMsg = PlayerBasePb.parsePlayerBaseTempMsg(crossBanquetData.getUserBaseInfo());
        temp.setUserBaseInfo(baseMsg);
        msg.addBanquetList(temp);
        return msg;
    }


    public static CrossBanquetProto.SyncCrossBanquetMemberMsg.Builder parseSyncCrossBanquetMemberMsg(CrossBanquetData crossBanquetData, CrossBanquetMember crossBanquetMember) {
        CrossBanquetProto.SyncCrossBanquetMemberMsg.Builder msg = CrossBanquetProto.SyncCrossBanquetMemberMsg.newBuilder();
        CrossBanquetProto.SyncCrossBanquetMemberTemp.Builder temp = CrossBanquetProto.SyncCrossBanquetMemberTemp.newBuilder();
        temp.setServerId(crossBanquetData.getServerId());
        temp.setUserId(crossBanquetMember.getUserId());
        temp.setBanquetId(crossBanquetMember.getBanquetId());
        temp.setGiftId(crossBanquetMember.getGiftId());
        temp.setPopularity(crossBanquetMember.getPopularity());
        temp.setIntegral(crossBanquetMember.getIntegral());
        temp.setPos(crossBanquetMember.getPos());
        temp.setRobot(crossBanquetMember.isRobot());
        temp.setRobotProcessId(crossBanquetMember.getRobotProcessId());
        temp.setRobotId(crossBanquetMember.getRobotId());
        PlayerProto.PlayerBaseTempMsg.Builder baseMsg = PlayerBasePb.parsePlayerBaseTempMsg(crossBanquetMember.getUserBaseInfo());
        temp.setUserBaseInfo(baseMsg);
        temp.setAllIntegral(crossBanquetData.getIntegral());
        temp.setAllPopularity(crossBanquetData.getPopularity());
        temp.setJoinTime(crossBanquetMember.getJoinTime());
        msg.addMemberList(temp);
        return msg;
    }


    public static BanquetProto.BanquetGetRecordResp.Builder parseBanquetGetRecordResp(List<CrossBanquetRecord> banquetRecordList) {
        BanquetProto.BanquetGetRecordResp.Builder msg = BanquetProto.BanquetGetRecordResp.newBuilder();
        for (CrossBanquetRecord info : banquetRecordList) {
            CrossBanquetData userBanquetData = info.getRecordObjcet();
            msg.addRecordList(parseBanquetRecordTempMsg(userBanquetData));
        }
        return msg;
    }

    public static BanquetProto.BanquetRecordTempMsg.Builder parseBanquetRecordTempMsg(CrossBanquetData data) {
        BanquetProto.BanquetRecordTempMsg.Builder msg = BanquetProto.BanquetRecordTempMsg.newBuilder();
        msg.setBanquetId(data.getBanquetId());
        msg.setType(data.getType());
        msg.setOpenTime((int) (data.getOpenTime() / 1000));
        msg.setInfo(data.getInvitationInfo());
        msg.setUserId(data.getUserId());
        msg.setPopularity(data.getPopularity());
        msg.setIntegral(data.getIntegral());
        msg.setMemberCount(data.getUserCount());
        PlayerProto.PlayerBaseTempMsg.Builder baseMsg = PlayerBasePb.parsePlayerBaseTempMsg(data.getUserBaseInfo());
        msg.setPlayerBaseData(baseMsg);
        return msg;
    }

    public static BanquetProto.MemberRewardTempMsg.Builder parseMemberRewardTempMsg(CrossBanquetMember memberInfo) {
        BanquetProto.MemberRewardTempMsg.Builder msg = BanquetProto.MemberRewardTempMsg.newBuilder();
        msg.setUserId(memberInfo.getUserId());
        msg.setPopularity(memberInfo.getPopularity());
        msg.setIntegral(memberInfo.getIntegral());
        msg.setIsRobot(memberInfo.isRobot());
        msg.setGiftId(memberInfo.getGiftId());
        msg.setRobotId(memberInfo.getRobotId());
        PlayerProto.PlayerBaseTempMsg.Builder baseMsg = PlayerBasePb.parsePlayerBaseTempMsg(memberInfo.getUserBaseInfo());
        msg.setPlayerBaseData(baseMsg);
        msg.setPos(memberInfo.getPos());
        return msg;
    }

    public static BanquetProto.BanquetTemp.Builder parseBanquetDataTempMsg(CrossBanquetData party, boolean join, boolean isKinship, boolean isKuafu) {
        BanquetProto.BanquetTemp.Builder temp = BanquetProto.BanquetTemp.newBuilder();
        temp.setUserId(party.getUserId());
        temp.setBanquetId(party.getBanquetId());
        temp.setType(party.getType());
        temp.setOpenTime((int) (party.getOpenTime() / 1000));
        temp.setPopularity(party.getPopularity());
        temp.setNum(party.getUserCount());
        //婚宴必须知道美女的亲密度
        if (party.getType() == eBanquetInfoType.MarryBanquet.getValue()) {
            BanquetMarryInvitation inv = JSON.parseObject(party.getInvitationInfo(), BanquetMarryInvitation.class);
            party.setInvitationInfo(JSON.toJSONString(inv));
        }
        temp.setInvitation(party.getInvitationInfo());
        PlayerProto.PlayerBaseTempMsg.Builder baseMsg = PlayerBasePb.parsePlayerBaseTempMsg(party.getUserBaseInfo());
        temp.setPlayerBaseData(baseMsg);
        temp.setSelfJoin(join);
        temp.setIsKinship(isKinship);
        List<Long> list = CrossPartyMgr.getJoinPartyList(party.getBanquetId());
        temp.addAllJoinList(list);
        if (isKinship) {
            isKuafu = false;
        }
        temp.setIskuafu(isKuafu);
        temp.setGiftTypeAccept(party.getGiftTypeAccept());
        temp.setMessage(party.getMessage());
        temp.setOverTime(party.getOverTime() / 1000);
        return temp;
    }

    public static BanquetProto.BanquetRelationshipRespMsg.Builder parseBanquetRelationshipRespMsg(List<CrossPartyRelationship> dataMap) {
        BanquetProto.BanquetRelationshipRespMsg.Builder resp = BanquetProto.BanquetRelationshipRespMsg.newBuilder();
        for (CrossPartyRelationship info : dataMap) {
            BanquetProto.BanquetRelationshipTemp.Builder temp = BanquetProto.BanquetRelationshipTemp.newBuilder();
            temp.setUserId(info.getUserId());
            temp.setTo(info.getTo());
            temp.setFrom(info.getFrom());
            if (!StringUtils.isNullOrEmpty(info.getBanquetId())) {
                temp.setBanquetId(info.getBanquetId());
            }
            if (info.getUserBaseInfo() != null) {
                if (info.getUserBaseInfo() != null) {
                    PlayerProto.PlayerBaseTempMsg.Builder baseMsg = PlayerBasePb.parsePlayerBaseTempMsg(info.getUserBaseInfo());
                    temp.setPlayerBaseData(baseMsg);
                }
            }
            resp.addShipList(temp);
        }
        return resp;
    }

    public static BanquetProto.BanquetFeastSync.Builder parseBanquetFeastListRespMsg(List<CrossBanquetData> banquetBeast) {
        BanquetProto.BanquetFeastSync.Builder msg = BanquetProto.BanquetFeastSync.newBuilder();
        for (CrossBanquetData info : banquetBeast) {
            msg.addBanquetDataList(parseBanquetDataTempMsg(info, false, false, false));
        }
        return msg;
    }

    public static BanquetProto.BanquetMemberInfoResp.Builder parseBanquetMemberListRespMsg(long serverId, Long userId, CrossBanquetData banquet, Map<Long, CrossBanquetMember> userBanquetMemberMap, int length) {
        BanquetProto.BanquetMemberInfoResp.Builder msg = BanquetProto.BanquetMemberInfoResp.newBuilder();
        msg.setBanquetInfo(parseBanquetDataTempMsg(banquet, userBanquetMemberMap.containsKey(userId), false, serverId != banquet.getServerId()));
        msg.setCount(length);
        for (CrossBanquetMember info : userBanquetMemberMap.values()) {
            msg.addMemberList(parseBanquetMemberTempMsg(info));
        }
        return msg;
    }

    public static BanquetProto.BanquetMemberTemp.Builder parseBanquetMemberTempMsg(CrossBanquetMember info) {
        BanquetProto.BanquetMemberTemp.Builder temp = BanquetProto.BanquetMemberTemp.newBuilder();
        temp.setUserId(info.getUserId());
        temp.setPos(info.getPos());
        temp.setPopularity(info.getPopularity());
        temp.setIsRobot(info.isRobot());
        temp.setRobotId(info.getRobotId());
        temp.setGiftId(info.getGiftId());
        PlayerProto.PlayerBaseTempMsg.Builder baseMsg = PlayerBasePb.parsePlayerBaseTempMsg(info.getUserBaseInfo());
        temp.setPlayerBaseData(baseMsg);
        temp.setJoinTime(info.getJoinTime());
        return temp;
    }

    public static BanquetProto.BanquetMemberSync.Builder parseBanquetMemberSyncResp(CrossBanquetData userBanquetData, CrossBanquetMember userBanquetMember, int count) {
        BanquetProto.BanquetMemberSync.Builder msg = BanquetProto.BanquetMemberSync.newBuilder();
        msg.setBanquetId(userBanquetData.getBanquetId());
        msg.setPopularity(userBanquetData.getPopularity());
        msg.setCount(count);
        msg.addMemberList(parseBanquetMemberTempMsg(userBanquetMember));
        return msg;
    }


    public static BanquetProto.BanquetRecordTempMsg.Builder parseCrossUserBanquetRecordTempMsg(CrossBanquetData data){
        BanquetProto.BanquetRecordTempMsg.Builder msg = BanquetProto.BanquetRecordTempMsg.newBuilder();
        msg.setBanquetId(data.getBanquetId());
        msg.setType(data.getType());
        msg.setOpenTime((int)(data.getOpenTime()/1000));
        msg.setInfo(data.getInvitationInfo());
        msg.setUserId(data.getUserId());
        msg.setPopularity(data.getPopularity());
        msg.setIntegral(data.getIntegral());
        msg.setMemberCount(data.getUserCount());
        PlayerProto.PlayerBaseTempMsg.Builder baseMsg = PlayerBasePb.parsePlayerBaseTempMsg(data.getUserBaseInfo());
        msg.setPlayerBaseData(baseMsg);
        for(CrossBanquetMember member : data.getMemberMap().values()){
            msg.addMemberRewardList(parseCrossMemberRewardTempMsg(member));
        }
        return msg;
    }

    public static BanquetProto.MemberRewardTempMsg.Builder parseCrossMemberRewardTempMsg(CrossBanquetMember memberInfo){
        BanquetProto.MemberRewardTempMsg.Builder msg = BanquetProto.MemberRewardTempMsg.newBuilder();
        msg.setUserId(memberInfo.getUserId());
        msg.setPopularity(memberInfo.getPopularity());
        msg.setIntegral(memberInfo.getIntegral());
        msg.setIsRobot(memberInfo.isRobot());
        msg.setGiftId(memberInfo.getGiftId());
        msg.setRobotId(memberInfo.getRobotId());
        PlayerProto.PlayerBaseTempMsg.Builder baseMsg = PlayerBasePb.parsePlayerBaseTempMsg(memberInfo.getUserBaseInfo());
        msg.setPlayerBaseData(baseMsg);
        msg.setPos(memberInfo.getPos());
        return msg;
    }


    public static BanquetProto.BanquetFeastSync.Builder parseBanquetFeastListRespMsg(long userId, Collection<CrossBanquetData> userBanquetDataMap){
        BanquetProto.BanquetFeastSync.Builder msg = BanquetProto.BanquetFeastSync.newBuilder();
        for(CrossBanquetData info : userBanquetDataMap){
            if(info.getUserId() == userId){
                continue;
            }
            Map<Long, CrossBanquetMember> partyMemberMap = CrossPartyMgr.getPartyMemberMap(info.getBanquetId());
            boolean isJoin = false;
            if (partyMemberMap != null) {
                isJoin = partyMemberMap.containsKey(userId);
            }
            msg.addBanquetDataList(parseBanquetDataTempMsg(info,isJoin, false, false));
        }
        return msg;
    }

    public static BanquetProto.BanquetJoinNoticeOwnerMsg.Builder parseBanquetJoinNoticeOwnerMsg(CrossBanquetMember userBanquetMember){
        BanquetProto.BanquetJoinNoticeOwnerMsg.Builder msg = BanquetProto.BanquetJoinNoticeOwnerMsg.newBuilder();
        msg.setUserId(userBanquetMember.getUserId());
        msg.setPos(userBanquetMember.getPos());
        msg.setPopularity(userBanquetMember.getPopularity());
        msg.setGiftId(userBanquetMember.getGiftId());
        PlayerProto.PlayerBaseTempMsg.Builder baseMsg = PlayerBasePb.parsePlayerBaseTempMsg(userBanquetMember.getUserBaseInfo());
        msg.setPlayerBaseData(baseMsg);
        return msg;
    }
}
